#region Namespaces

using System;
using System.Diagnostics;
using System.Net;

#endregion

namespace Vibstudio.X4NET.Xml.Navigation
{
    internal interface IStringsValidator
    {
        bool IsValid(string stringToValidate);
    }

    public enum ValidationCriterion
    {
        Http
    }

    public class StringsValidator
    {
        private readonly IStringsValidator _validator;

        public StringsValidator(ValidationCriterion criteria)
        {
            Type type = Type.GetType(string.Format("{0}.{1}Validator", GetType().Namespace, criteria));
            _validator = (IStringsValidator) Activator.CreateInstance(type);
        }

        public bool IsValid(string stringToValidate)
        {
            return _validator.IsValid(stringToValidate);
        }
    }

    internal class HttpValidator : IStringsValidator
    {
        public bool IsValid(string uri)
        {
            string[] uriList = new[] { uri, string.Format("{0}?WSDL", uri), string.Format("{0}?wsdl", uri) };

            foreach (string currUri in uriList)
            {
                HttpWebResponse response = null;
                try
                {
                    var request = (HttpWebRequest)WebRequest.Create(currUri);
                    request.KeepAlive = false;
                    response = request.GetResponse() as HttpWebResponse;
                    return true;
                }
                catch (Exception e)
                {
                    Trace.WriteLine(string.Format("Exception message: {0}", e.Message), "UtilitiesManager.IsValidURI");
                }
                finally
                {
                    if (response != null) response.Close();
                }
            }
            return false;
        }
    }
}